<template>
  <!-- 店主认证 -->
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
      :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="mb-[10px]" v-show="showSearch">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="68px">
            <el-form-item label="用户ID" prop="userId">
              <el-input v-model.trim="queryParams.userId" placeholder="请输入" clearable style="width: 200px" />
            </el-form-item>
            <el-form-item label="手机号" prop="mobile">
              <el-input v-model.trim="queryParams.mobile" placeholder="请输入" clearable style="width: 200px" />
            </el-form-item>
            <el-form-item label="状态" prop="status">
              <el-select v-model="queryParams.status" placeholder="请选择" clearable style="width: 105px">
                <el-option label="待审核" value="1" />
                <el-option label="审核通过" value="2" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleQuery" icon="Search">搜索</el-button>
              <el-button @click="resetQuery" icon="Refresh">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>
    <el-card shadow="hover">
      <!-- 功能按钮 -->
      <template #header>
        <el-row :gutter="10">
          <el-col :span="1.5">
            <el-button type="warning" plain :disabled="multiple" icon="Open" @click="handleOpen()"
              v-hasPermi="['ums:store:audit']">审核</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :search="true"
            :columns="columns"></right-toolbar>
        </el-row>
      </template>
      <!-- 表格 -->
      <el-table ref="supTableRef" v-loading="loading" :data="storeList" max-height="550" border
        @selection-change="handleSelectionChange">
        <el-table-column fixed="left" type="selection" :selectable="(row: any) => row.storeAuditFlag === 1" width="55"
          align="center" />
        <el-table-column label="用户" align="center" width="350" v-if="columns[0].visible">
          <template #default="scope">
            <el-row justify="space-evenly" align="middle">
              <el-image style="width: 80px; height: 80px"
                :src="scope.row.avatar "
                :preview-src-list="[scope.row.avatar]" fit="contain" :preview-teleported="true">
                <template #error>
                  <div></div>
                </template>
              </el-image>
              <el-col :span="17">
                <div class="lioneuo">
                  <el-row>
                    <el-col :span="7" class="cuionseti">ID：</el-col>
                    <el-col :span="17">{{ scope.row.id }}</el-col>
                  </el-row>
                  <el-row v-if="scope.row.sn">
                    <el-col :span="7" class="cuionseti">SN：</el-col>
                    <el-col :span="17">{{ scope.row.sn }}</el-col>
                  </el-row>
                  <el-row>
                    <el-col :span="7" class="cuionseti">昵称：</el-col>
                    <el-col :span="17">{{ scope.row.nickname }}</el-col>
                  </el-row>
                  <el-row v-if="scope.row.userCode">
                    <el-col :span="7" class="cuionseti">邀请码：</el-col>
                    <el-col :span="17">{{ scope.row.userCode }}</el-col>
                  </el-row>
                  <el-row>
                    <el-col :span="7" class="cuionseti">手机号：</el-col>
                    <el-col :span="17">{{ scope.row.mobile }}</el-col>
                  </el-row>
                </div>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column label="身份信息" align="center" width="230" v-if="columns[1].visible">
          <template #default="scope">
            <div class="lioneuo">
              <el-row v-if="scope.row.realName">
                <el-col :span="7" class="cuionseti">姓名：</el-col>
                <el-col :span="17">{{ scope.row.realName }}</el-col>
              </el-row>
              <el-row v-if="scope.row.idCard">
                <el-col :span="7" class="cuionseti">身份证：</el-col>
                <el-col :span="17">{{ scope.row.idCard }}</el-col>
              </el-row>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="店铺信息" align="center" width="325" v-if="columns[2].visible">
          <template #default="scope">
            <el-row justify="space-evenly" align="middle">
              <el-image style="width: 80px; height: 80px"
                :src="scope.row.storeAvatar "
                :preview-src-list="[scope.row.storeAvatar]" fit="contain" :preview-teleported="true">
                <template #error>
                  <div></div>
                </template>
              </el-image>
              <el-col :span="17">
                <div class="lioneuo">
                  <el-row v-if="scope.row.storeName">
                    <el-col :span="10" class="cuionseti">名称：</el-col>
                    <el-col :span="14">{{ scope.row.storeName }}</el-col>
                  </el-row>
                  <el-row>
                    <el-col :span="10" class="cuionseti">审核状态：</el-col>
                    <el-col :span="14">{{ storeFilters(scope.row.storeAuditFlag) }}</el-col>
                  </el-row>
                  <el-row v-if="scope.row.storeAuditTime">
                    <el-col :span="10" class="cuionseti">开通时间：</el-col>
                    <el-col :span="14">{{ scope.row.storeAuditTime.slice(0, -3) }}</el-col>
                  </el-row>
                </div>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column label="银行信息" align="center" width="260" v-if="columns[3].visible">
          <template #default="scope">
            <div class="lioneuo">
              <el-row v-if="scope.row.bankName">
                <el-col :span="8" class="cuionseti">名称：</el-col>
                <el-col :span="16">{{ scope.row.bankName }}</el-col>
              </el-row>
              <el-row v-if="scope.row.bankAccount">
                <el-col :span="8" class="cuionseti">卡号：</el-col>
                <el-col :span="16">{{ scope.row.bankAccount }}</el-col>
              </el-row>
              <el-row v-if="scope.row.bankMobile">
                <el-col :span="8" class="cuionseti">手机号：</el-col>
                <el-col :span="16">{{ scope.row.bankMobile }}</el-col>
              </el-row>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="直推/间推" align="center" width="260" v-if="columns[4].visible">
          <template #default="scope">
            <div class="lioneuo">
              <el-row>
                <el-col :span="8" class="cuionseti">直推用户：</el-col>
                <el-col :span="16">{{ scope.row.directUserId }}</el-col>
              </el-row>
              <el-row>
                <el-col :span="8" class="cuionseti">间推用户：</el-col>
                <el-col :span="16">{{ scope.row.indirectUserId }}</el-col>
              </el-row>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="所属部门ID" prop="agentId" align="center" width="140" v-if="columns[5].visible" />
        <el-table-column fixed="right" label="创建时间" prop="createTime" align="center" width="140">
          <template #default="scope">{{ scope.row.createTime.slice(0, -3) }}</template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" align="center" width="80" v-hasPermi="['ums:store:audit']">
          <template #default="scope">
            <el-tooltip :content="`${scope.row.storeAuditFlag === 2 ? '已' : ''}审核`" placement="top">
              <el-button link type="primary" icon="Open" :disabled="scope.row.storeAuditFlag === 2"
                @click="handleOpen(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 表格 -->
      <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
  </div>
</template>

<script setup lang="ts" name="Shopkeeper">
import { getCurrentInstance, onBeforeMount, ref } from "vue";
import { listStore, updateGoods } from '@/api/operates/shopkeeper/index';
import { StoreQuery, StoreVO } from '@/api/operates/shopkeeper/types';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const showSearch = ref(true)
const loading = ref(true)
const ids = ref<Array<string | number>>([])
const multiple = ref(true)
const total = ref(0)
// 查询表单
const queryParams = ref<StoreQuery>({
  pageNum: 1,
  pageSize: 10,
});
// 表格数据
const storeList = ref<StoreVO[]>([]);
const queryFormRef = ref<ElFormInstance>();

const columns = ref<FieldOption[]>([
  { key: 0, label: `用户`, visible: true, children: [] },
  { key: 1, label: `身份信息`, visible: true, children: [] },
  { key: 2, label: `店铺信息`, visible: true, children: [] },
  { key: 3, label: `银行信息`, visible: true, children: [] },
  { key: 4, label: `直推/间推`, visible: true, children: [] },
  { key: 5, label: `所属部门ID`, visible: true, children: [] },
])

/**
 * 查询店主列表
 */
const getList = async () => {
  loading.value = true
  const query = { ...queryParams.value }
  const res = await listStore(query);
  storeList.value = res.data.list;
  total.value = res.data.total;
  loading.value = false;
}

/**
 * 搜索按钮操作
 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/**
 * 重置搜索
 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/**
 * 审核
 */
const handleOpen = async (row?: StoreVO) => {
  try {
    const id = row?.id || ids.value.join(',');
    await proxy?.$modal.confirm(`是否确认进行审核`);
    await updateGoods(id);
    getList()
    proxy?.$modal.msgSuccess("操作成功");
  } catch (error) {
  }
}

/**
 * 多选框选中数据
 * @param selection 
 */
const handleSelectionChange = (selection: StoreVO[]) => {
  ids.value = selection.map((item: StoreVO) => item.id);
  multiple.value = !selection.length;
}

const storeFilters = (val: number) => {
  if (val === 1) {
    return '审核中'
  } else if (val === 2) {
    return '审核通过'
  }
}

onMounted(() => {
  getList();
});

</script>

<style scoped></style>